<?php
namespace app\controller\handler;
use app\controller\Common;
use think\Db;
use think\Model;

class Xiongzhang extends Common{

  public function getConfigList(){
    $base = input('post.');
    $needArr = explode(',',$base['need']);
    $res = model('XiongzhangConfig')::all(['status'=>1]);
    if($res){
      $tmp = [];
      foreach($res as $value){
        if(in_array($value['title'],$needArr)){
          $tmp[$value['title']] = $value['content'];
        }
      }

      if(count($tmp) > 0){
        $result['ret'] = 1;
        $result['data'] = $tmp;
      }else{
        $result['ret'] = 0;
        $result['msg'] = '未获取到数据';
      }
    }else{
      $result['ret'] = 0;
      $result['msg'] = '无有效配置参数';
    }
    return $result;
  }

  public function editConfig(){
    $base = input('post.');
    $wh['title'] = $base['title'];
    $up['content'] = $base['content'];
    $res = model('XiongzhangConfig')::where($wh)->update($up);
    if($res){
      $result['ret'] = 1;
      $result['msg'] = '编辑成功';
    }else{
      $result['ret'] = 0;
      $result['msg'] = '编辑失败';
    }
    return $result;
  }

  public function verify(){
    $base = input('get.');
    $signature = $base["signature"];
    $timestamp = $base["timestamp"];
    $nonce = $base["nonce"];
    $echostr = $base["echostr"];
    $token = "yoki";
    $tmpArr = array($token, $timestamp, $nonce);
    sort($tmpArr, SORT_STRING);
    $tmpStr = implode( $tmpArr );
    $tmpStr = sha1( $tmpStr );
    if( $tmpStr == $signature ){
      echo  $echostr;
    }else{
      echo  false;
    }
  }

  //获取access_token
  public function getAccessToken(){
    if(!session('?xz_access_token') || session('xz_access_token')==0 || session('xz_access_token_expires')<=time()){
      $res_client_id = model('XiongzhangConfig')::get(['title'=>'client_id','status'=>1]);
      if(!$res_client_id){
        $result['ret'] = 0;
        $result['msg'] = '未获取到开发者ID';
        return $result;
      }else{
        $client_id = $res_client_id['content'];
      }
      $res_client_secret = model('XiongzhangConfig')::get(['title'=>'client_secret','status'=>1]);
      if(!$res_client_secret){
        $result['ret'] = 0;
        $result['msg'] = '未获取到开发者密码';
        return $result;
      }else{
        $client_secret = $res_client_secret['content'];
      }
      $url = 'https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id='.$client_id.'&client_secret='.$client_secret;
      $res = curl_get($url);
      if(isset($res['error'])){
        session('xz_access_token',0);
        $result['ret'] = 0;
        $result['msg'] = $res['error_description'];
        return $result;
      }else{
        session('xz_access_token',$res['access_token']);
        session('xz_access_token_expires',time() + $res['expires_in']);
        session('xz_scope',$res['scope']);
      }
    }
    $result['ret'] = 1;
    $result['msg'] = 'OK';
    return $result;
  }

  //获取已添加的模板列表
  public function getMyTemplateList(){
    $res_token = $this->getAccessToken();
    if($res_token['ret'] == 0){
      return json($res_token);
    }else{
      $access_token = session('xz_access_token');
      $url = 'https://openapi.baidu.com/rest/2.0/cambrian/template/get_all_private_template?access_token='.$access_token;
      $res = curl_get($url);
      if(count($res['template_list'])>0){
        $result['ret'] = 1;
        $result['data'] = $res['template_list'];
      }else{
        $result['ret'] = 0;
        $result['msg'] = '未查询到相关数据';
      }
    }
    return json($result);
  }

  //获取系统模板列表
  public function getSystemTemplateList(){
    $res_token = $this->getAccessToken();
    if($res_token['ret'] == 0){
      return $res_token;
    }else{
      $access_token = session('xz_access_token');
      $url = 'https://openapi.baidu.com/rest/2.0/cambrian/template/sys_list?access_token='.$access_token;
      $data = [
        'kw'=>'接收时间/接收条数'
      ];
      $res = curl_post($url,$data);
      if(count($res['template_list'])>0){
        $result['ret'] = 1;
        $result['data'] = $res['template_list'];
      }else{
        $result['ret'] = 0;
        $result['msg'] = '未查询到相关数据';
      }
    }
    return json($result);
  }



}//END